/**
 * Created by hcadmin on 16/10/16.
 */
$(function(){
    //获取url的参数
    (function ($) {
        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
    })(jQuery);
    var title     = unescape($.getUrlParam('title')) ;
    var id = $.getUrlParam('id');
    var token           = localStorage.token;               //获取分享过来的 token;
    var userId          = localStorage.user_id;              //用户id

    $(".title").html(title);

    $.ajax({
        url:apiurl+"/dailyrecords/"+id+"",
        type:"GET",
        data:{
            user_id:userId,
            token:token
        },
        success:function(d){
            if(typeof d == "object"){
                var msg= d.msg;
                if(msg=="202") {
                    $(".token_iframe").fadeIn(1000);
                }
            }else{
                var data        = JSON.parse(d).data;

                var userInfo    = data.userCallbackString;

                var follows                 =data.followerRelations;                                   //获取抄送人

                //添加 已读
                //console.log(follows);
                var readers = [];
                if(follows.length>0){
                    for(var i = 0; i < follows.length; i ++){
                        var is_read = follows[i].is_read;
                        if(is_read == "1"){
                            var reader_id = follows[i].user_id;

                            for(var j = 0; j < userInfo.length; j ++){
                                if(userInfo[j].id == reader_id){
                                    readers.push(userInfo[j]);
                                }
                            }
                        }
                    }
                }

                var num=0;
                var person_html = "";
                if(readers.length>0){
                    for(var i = 0; i < readers.length; i++){
                        var reader = readers[i];

                        var img_src;
                        //if(avatar_arr[i]==""){
                        var color = avatar_getColor(reader.qyxBindId);
                        img_src = "<div class='had_read_person_head_div' style='background-color: "+color+"'>"+avatar_name(reader.name)+"</div>";
                        //}else{
                        if (reader.avatar.length > 0){
                            var src = avatar_getUrl(localStorage.user_id,localStorage.token,reader.avatar);
                            img_src += "<img src='"+src+"' class='had_read_person_head' hidden>";
                        }
                        num++;
                        person_html+="<div class='had_read_person_big'>"+img_src+"<div class='had_read_person_name'>"+reader.name+"</div></div>";
                    }
                }
                var num_html = "<div class='read_text'>已读("+num+")</div>";
                $(".down_navbar_div").append(num_html+person_html);


                $(".had_read_person_head").each(function(){
                    var parent = $(this).parent();
                    var parentWidth = parent.width();
                    var currentWidth = $(this).width();

                    var marginLeft = (parentWidth - currentWidth)/2;

                    $(this).css("margin-left",marginLeft + "px");
                });

                $(".had_read_person_head_div").each(function(){
                    var parent = $(this).parent();
                    var parentWidth = parent.width();
                    var currentWidth = $(this).width();

                    var marginLeft = (parentWidth - currentWidth)/2;

                    $(this).css("margin-left",marginLeft + "px");
                });


                showAvatar(".had_read_person_head");
            }
        }
    });


    function showAvatar(classname){
        $(classname).one("load",function(){
            $(this).show();
            $(this).prev().hide();
        });
    }


});